<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaProcessFlowController extends OaBaseController {
    protected $model;
    protected $token;
    function _initialize() {
        $this->model = $this->getModel ( 'oa_process_flow' );
        $this->token = get_token();

        parent::_initialize ();
        $controller = strtolower ( CONTROLLER_NAME );
        $action = strtolower(ACTION_NAME);

        $res ['title'] = '流程管理';
        $res ['url'] = addons_url ( 'OaSystem://OaProcessFlow/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oaprocessflow' ? 'cur' : '';
        $nav [] = $res;
        $this->assign ( 'nav', $nav );

        $res ['title'] = '流程列表';
        $sub_nav[] = $res;
        $res ['title'] = '流程类型';
        $res ['url'] = addons_url ( 'OaSystem://OaApplyType/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oaapplytype' ? 'cur' : '';
        $sub_nav[] = $res;
        $this->assign ( 'sub_nav', $sub_nav );
    }

    public function lists(){
        $list_data = $this->_get_model_list ( $this->model );
        $grids = $list_data['list_grids'];
        //$data = $list_data['list_data'];
        $data = db_select('oa_process_flow');
        foreach ($data as $i => $v) {
            $flowNodes = json_decode($v['operations'], true);
            $operationFlowName = '';
            foreach ($flowNodes as $id) {
                if($v['type'] == 3) {
                    $node = db_find('oa_node', ['id'=>$id]);
                    if($node != NULL) {
                        $operationFlowName .= $node['name'];
                        $operationFlowName .= ($id == end($flowNodes)) ? ' ' : ' -> ';
                    }
                } else {
                    $role = db_find('oa_role', ['id'=>$id]);
                    if($role != NULL) {
                        $operationFlowName .= $role['name'];
                        $operationFlowName .= ($id == end($flowNodes)) ? ' ' : ' -> ';
                    }
                }
                $data[$i]['operations'] = $operationFlowName;

                //$editUrl = U('edit', array('id'=> $v['id']));
                $delUrl = U('del', array('id'=> $v['id']));
                //$data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
                $data[$i]['urls'] = "<a class='confirm' href=$delUrl>删除</a>";
            }
            $applyType = db_find('oa_apply_type', ['id'=>$v['type']]);
            $data[$i]['type'] = $applyType['name'];
         }
        $this->assign('list_grids', $grids);
        $this->assign('list_data', $data);
        $this->display();
    }

    public function details(){
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['name'] = $_POST['name'];
            $data['type'] = $_POST['type'];
            $data['attr'] = $_POST['attr'];

            if(db_find('oa_process_flow', ['name'=>$data['name']])) {
                $this->error('该流程已经存在, 请重新输入！');
                return false;
            }

            $operations = array();
            for ($i = 1; $i <= 7; $i++) {
                $operationStep = 'operation'.$i;
                $operationStepId = intval($_POST[$operationStep]);
                if($operationStepId > 0)
                    array_push($operations, $operationStepId);
                else
                    break;
            }
            $data['operations'] = json_encode($operations);

            M('oa_process_flow')->add($data);

            $this->success ( '创建' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));

        } else {
            $node = db_select('oa_node');
            $applyType = db_select('oa_apply_type');
            $tmp = array('id' => 0, 'name' => '无');
            array_unshift($node, $tmp);
            $this->assign('node', $node);
            $this->assign('applyType', $applyType);
            $this->display('add');
        }
    }

    public function edit(){
        parent::edit($this->model);
    }

}
